// pages/authorization/authorization.js
const app = getApp()
const ajax = require('../../utils/ajax.js')
// TODO: 接口替换logo,文字描述
Page({
  /**
   * 页面的初始数据
   */
  data: {
    params: {}
  },

  onGetUserInfo(e) {
    wx.showLoading({
      title: '请求中...'
    })
    wx.getUserInfo({
      withCredentials: true,
      success: res => {
        if (res.errMsg.indexOf('ok') > -1) {
          let rawData
          try {
            rawData = JSON.parse(res.rawData)
          } catch (error) {
            throw Error('转换json数据出错')
          }
          ajax
            .wxRequest({
              url: '/index.php/Api/SApi/index/m/updateUserInfoXCX',
              data: {
                nickName: rawData.nickName,
                head_pic: rawData.avatarUrl,
                sex: rawData.gender,
                encryptedData: res.encryptedData,
                iv: res.iv
              }
            })
            .then(res => {
              console.log("222")
              console.log(res)
              if (res.user_info) {
                let userInfo = res.user_info
                for(let i in userInfo) {
                  if(i == 'id' || i == 'vip_id' || i == 'customerid') {
                    // wx.setStorageSync({
                    //   key: i,
                    //   data: userInfo[i]
                    // })
                    console.log(i)
                    console.log(userInfo[i])
                    wx.setStorageSync(i, userInfo[i])
                    console.log(wx.getStorageSync(i))
                  }
                }
              }
              wx.switchTab({
                url: '/pages/index/index',
              })
              wx.hideLoading()
            })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {}
})
